/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clasificador.redneuronal.neurona;


/**
 *
 * @author e237573
 */
public class NeuronaDiscreta extends Neurona {
    double[] weights;
   
    @Override
    public double errorFunction(double[] errors) {
    	double error = 0.0;
    	
    	for(int i = 0; i < errors.length; i++)
    	{
    		error += errors[i];
    	}
    	
    	return error;
    }

    @Override
    public double activationFunction(double[] inputs) {
        //Logger.info("Calculando activacion discreta para entradas " + Arrays.toString(inputs));
        //Logger.info("Con pesos " + Arrays.toString(weights));
        double sum = weights[0];
        for( int i = 0; i < inputs.length; i++ ) {
            sum += weights[i+1] * inputs[i];
        }
        //Logger.info("Suma de pesos: " + sum);
        return (sum > 0? 1: 0);
    }

    @Override
    public double derivative(int i, double[] inputs, double output) {
        return 0.0;
    }

    @Override
    public void train(double learningCoefficient) {
        //Logger.info("Entrenando neurona con coeficiente de aprendizaje " + learningCoefficient);
        double diff = learningCoefficient * this.errorSignal.getValue();        
        //Logger.info("Error: " + this.errorSignal.getValue());
        //Logger.info("Eta: " + learningCoefficient);
        //Logger.info("Error por eta: " + diff);
        //Logger.warning("Pesos antes de la actualizacion: " + Arrays.toString(weights));
        this.weights[0] -= diff;
        for( int i = 0; i < this.weights.length - 1; i++ ) {
            this.weights[i + 1] -= diff * this.getInputValue(i);
        }
        //Logger.warning("Pesos tras la actualizacion: " + Arrays.toString(weights));    
    }   
    
    @Override
    public void init()
    {
    	this.weights = new double[this.getNInputs()+1];
    	
    	weights[0] = 0.0;
    	
    	for(int i = 1; i <= this.getNInputs(); i++)
    	{
    		weights[i] = 1.0;
    	}
    }
}
